#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <string.h>
MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //this->showFullScreen();
    qDebug()<<"00000000";
    connectstate = false;
    register_page = new Register;
    this->setFixedSize(440,720);
    register_page->hide();
    connect_client();
    SocketManager::getinstance().initializeConnection();
    qDebug()<<"111111";

    page1_init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::connect_client()
{
    //调用单例模式的套接字对象
    socket = SocketManager::getinstance().socket();


    //连接成功信号返回提示
    connect(socket,&QTcpSocket::connected,this,[&](){
     //   ui->textEdit->append("成功连接到服务器");
        qDebug()<<"成功连接到服务器";
        connectstate =true;
    });
    //连接错误提示
    //connect(socket,QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error),this,[&](){
     //   ui->textEdit->append(socket->errorString());
    //});
    //连接断开提示
    connect(socket,&QTcpSocket::disconnected,this,[&](){
        connectstate =false;
    });

    //接收数据显示
    connect(socket,&QTcpSocket::readyRead,this,&MainWindow::start);


}
//页面操作
void MainWindow::page1_init()
{
    ui->label->setScaledContents(true);
    ui->label->setPixmap(QPixmap(":/new/logo.png"));

    courier_page = new Courier;
    courier_page->hide();
    qDebug()<<"------------";
    consignee_page = new Consignee;
    consignee_page->hide();
    qDebug()<<"222222222222";
    courier_main_page = new Courier_main;
    courier_main_page->hide();
    qDebug()<<"33333333333";
    courier_add_page = new Courier_add;
    courier_add_page->hide();
    qDebug()<<"4444444444444";
    consignee_main_page = new Consignee_main;
    consignee_main_page->hide();
    qDebug()<<"88888888888888";
    connect(courier_page,&Courier::returnregister,this,&MainWindow::returnRegister);
    connect(register_page,&Register::tocourier,this,&MainWindow::registertocourier);
    connect(register_page,&Register::toMain,this,&MainWindow::rgttomain);
    connect(courier_page,&Courier::rigfinish,this,&MainWindow::couriertomain);
    connect(consignee_page,&Consignee::cee_rigfinish,this,&MainWindow::consigneetomain);
    connect(register_page,&Register::toconsignee,this,&MainWindow::rgttoconsignee);
    connect(courier_add_page,&Courier_add::addtomain,this,&MainWindow::addtomain_courier);
    connect(consignee_page,&Consignee::returnreg,this,&MainWindow::consigneeToRegister);
    connect(courier_main_page,&Courier_main::maintoad,this,&MainWindow::maintoadd);
}

void MainWindow::start()
{
    if(!this->isActiveWindow()){
        return;
    }
    sendRig mes;
    while (socket->bytesAvailable() >= sizeof(sendRig)) {
            socket->read(reinterpret_cast<char*>(&mes), sizeof(sendRig));
    }
//    sendRig *mes;
//    mes = (sendRig*)socket->readAll().data();
    qDebug()<<"type:"<<mes.type;
    qDebug()<<"dl:"<<mes.IC;
    if(mes.type == 2){
        if(mes.IC==1)
        {
            //进入快递员主页面
//            QString* tele1 =new QString;
//            tele1->append(mes->tele);
//            Telephone_Number::teleinstance().changeTeleValues(tele1);
            qDebug()<<mes.tele;
            Telephone_Number::teleinstance().changeTeleValues(QString::fromUtf8(mes.tele));
            this->hide();
            courier_main_page->show();
            courier_add_page->hide();
        }else if (mes.IC == 2) {
            //进入用户主页面
//            QString* tele1 =new QString;
//            tele1->append(mes->tele);
//            Telephone_Number::teleinstance().changeTeleValues(tele1);
            Telephone_Number::teleinstance().changeTeleValues(mes.tele);
            this->hide();
            consignee_main_page->show();
        }else if(mes.IC == -1){
            QMessageBox::warning(this,"登录失败","账户不存在，请检查电话号码或者注册","返回");
        }else if (mes.IC == 0) {
            QMessageBox::warning(this,"登录失败","密码错误","返回");
        }else {
            QMessageBox::warning(this,"登录失败","登录失败，请稍后再试","返回");
        }
    }
}

//登录逻辑
void MainWindow::on_pushButton_2_clicked()
{
    sendRig sRg;
    sRg.type = 2;
    const char* telenum = ui->lineEdit->text().toUtf8().constData();
    const char* passw = ui->lineEdit_2->text().toUtf8().constData();
    strcpy(sRg.tele,telenum);
    strcpy(sRg.password,passw);
    sRg.IC=0;
    sRg.Idtype=false;
    strcpy(sRg.Account,"0");
    QByteArray senddata;
    senddata.append((char*)&sRg,sizeof (sendRig));
    socket->write(senddata);
}

void MainWindow::addtomain_courier()
{
    courier_add_page->hide();
    courier_main_page->show();
}

void MainWindow::maintoadd()
{
    courier_main_page->hide();
    courier_add_page->show();
}



void MainWindow::rgttomain()
{
    register_page->hide();
    this->show();
}



void MainWindow::on_pushButton_3_clicked()
{
    this->hide();
    register_page->show();
}

void MainWindow::returnRegister()
{
    courier_page->hide();
    register_page->show();
}

void MainWindow::registertocourier()
{

    register_page->hide();
    qDebug() << "Before show - Courier visible:" << courier_page->isVisible();
    qDebug() << "Courier size:" << courier_page->size();
    courier_page->show();
    courier_page->raise();  // 置顶
    courier_page->activateWindow();  // 激活
    qDebug()<<"xxxxxxxxxx";
}

void MainWindow::couriertomain()
{
    courier_page->hide();
    this->show();
}

void MainWindow::consigneetomain()
{
    consignee_page->hide();
    this->show();
}

void MainWindow::rgttoconsignee()
{
    register_page->hide();
    consignee_page->show();
}

void MainWindow::consigneeToRegister()
{
    consignee_page->hide();
    register_page->show();
}
